//
//  AppGCDTimer.swift
//  LeTu
//
//  Created by g1 on 2024/1/16.
//

import UIKit

typealias ActionBlock = () -> ()

class AppGCDTimer {

    //单例
    static let shared = AppGCDTimer()
    
    // tuple 0:Timer 1:Suspend 状态记录
    var timerContainer = [String: (DispatchSourceTimer,Bool)]()
    
    
    
    /// GCD定时器
    ///
    /// - Parameters:
    ///   - name: 定时器名字
    ///   - timeInterval: 时间间隔
    ///   - queue: 队列
    ///   - repeats: 是否重复
    ///   - action: 执行任务的闭包
    func scheduledDispatchTimer(WithTimerName name: String?, timeInterval: Double, queue: DispatchQueue, repeats: Bool, action: @escaping ActionBlock) {
        
        if name == nil {
            return
        }
        
        let tuple = timerContainer[name!]
        var timer = tuple?.0
        if timer == nil {
            timer = DispatchSource.makeTimerSource(flags: [], queue: queue)
            timer?.resume()
            timerContainer[name!] = (timer!,false)
        }
        //精度0.1秒
        timer?.schedule(deadline: .now(), repeating: timeInterval, leeway: DispatchTimeInterval.milliseconds(10))
        timer?.setEventHandler(handler: { [weak self] in
            action()
            if repeats == false {
                self?.cancelTimer(WithTimerName: name)
            }
        })
    }
    
    /// 取消定时器
    ///
    /// - Parameter name: 定时器名字
    func cancelTimer(WithTimerName name: String?) {
        
//        MYLog("cancleTimer - name: ",name)
        
        guard timerContainer.keys.count > 0, let key = name else { return }
        guard let tuple = timerContainer[key] else { return }
        
        let timer = tuple.0
        timer.cancel()
        timerContainer.removeValue(forKey: key)
        
    }
    
    
    /// 暂停定时器
    ///
    /// - Parameter name: 定时器名字
    func suspendTimer(WithTimerName name: String?) {
        
        guard timerContainer.keys.count != 0, let key = name else { return }
        guard var tuple = timerContainer[key] else { return }

        let timer = tuple.0
        if tuple.1 == false {
            timer.suspend()
            tuple.1 = true
            timerContainer[key] = (timer, true)
        }
    }
    
    /// 开始定时器
    ///
    /// - Parameter name: 定时器名字
    func startTimer(WithTimerName name: String?) {
        
        // MYLog("startTimer - name: ",name)
        
        guard timerContainer.keys.count != 0, let key = name else { return }
        guard var tuple = timerContainer[key] else { return }
        
        let timer = tuple.0
        if tuple.1 == true {
            timer.resume()
            tuple.1 = false
            timerContainer[key] = (timer, false)
        }

    }
    
    /// 获取定时器是否正在
    func isSuspend(WithTimerName name: String) -> Bool {
        
        guard timerContainer.keys.count != 0, let tuple = timerContainer[name] else { return true }
        return tuple.1
        
    }
    
    /// 检查定时器是否已存在
    ///
    /// - Parameter name: 定时器名字
    /// - Returns: 是否已经存在定时器
    func isExistTimer(WithTimerName name: String?) -> Bool {
        
        guard timerContainer.keys.count != 0 else { return false }
        
        if timerContainer[name!] != nil {
            return true
        }
        return false
    }
    
    
    
}
